package com.gec.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.gec.dao.BuycarDao;
import com.gec.dao.Impl.BuycarDaoImpl;
import com.gec.entity.Buycar;
import com.gec.entity.Good;
import com.gec.service.BuycarService;

public class BuycarServiceImpl implements BuycarService {
	private BuycarDao dao = new BuycarDaoImpl();

	@Override
	public Buycar findMeId(int id) {
		return dao.selectMeId(id);
	}

	@Override
	public Buycar findGoodId(int gid) {
		return dao.selectGoodId(gid);
	}

	@Override
	public int findtotal(int id, int sid) {
		return dao.total(id,sid);
	}

	@Override
	public void add(int id,int gid) {	
		Buycar bc = selectId(id,gid);
		if(bc != null) {
			/*bc.setQty(bc.getQty()+1);*/
			dao.update(bc);
			
		}else {
			Buycar bc1 = new Buycar();
			bc1.setGood_id(gid);
			bc1.setMem_id(id);
			dao.addbc(bc1);
		}
		
	}
	

	@Override
	public List<Buycar> showAll(int id, int sid) {
		return dao.showAll(id,sid);
	}

	@Override
	public Buycar selectNum(int id, int gid) {
		return dao.selectNum(id,gid);
	}

	@Override
	public Object selectSum(int id, int sid) {
		return dao.selectSum(id,sid);
	}

	@Override
	public Object remaove(int cbid) {
		return dao.delete(cbid);
	}

	@Override
	public float getMoneySum(int id) {
		int sum = 0;
		List<Buycar> list = new ArrayList<>();
		for (Buycar b : list) {
			sum += b.getSum();
		}
		return sum;
	}

	@Override
	public float findCur(int id) {
		int cur = this.findCurs(id);
		return 0;
	}

	private int findCurs(int id) {
		return dao.findCurs(id);
	}

	@Override
	public float getZjMoneySum(int id) {
//		int 
		return 0;
	}

	@Override
	public Buycar selectId(int mid, int sid) {
		return dao.selectId(mid, sid);
	}

	@Override
	public void addGoodNumBuyCarId(Integer integer) {
		dao.addsNum(integer);
	}

	@Override
	public int findById(Integer integer) {
		return dao.selectByIdQty(integer);
	}

	@Override
	public Buycar findBuyCarId(Integer integer) {
		return dao.selectById(integer);
	}

	@Override
	public float cost(int id, Integer integer) {
		float costPrice = 0;
		List<Buycar> list = dao.selectAllBuycar(id, integer);
		for (Buycar buyCar : list) {
			costPrice += buyCar.getSum();
		}
		return costPrice;
	}

	@Override
	public float discount(int id, Integer integer) {
		float discount = 0;
		BuycarServiceImpl impl = new BuycarServiceImpl();
		float countPrice = impl.cost(id, integer);
		List<Buycar> list = dao.selectAllBuycar(id, integer);
		for (Buycar c : list) {
			discount += c.getDiscount();
		}
		return countPrice - discount;
	}

	@Override
	public float disCountPrice(int id, Integer integer) {
		 BuycarServiceImpl b = new BuycarServiceImpl();
		return b.cost(id, integer) - b.discount(id, integer);
	}

	@Override
	public void cutGoodNumBuyCarId(Integer integer) {
		dao.cutGoodnum(integer);
	}
	
}
